import 'dart:convert';

import 'package:im_app/im/util/MapUtil.dart';
import 'package:logger/logger.dart';
import 'package:sqflite/sqflite.dart';

import '../pojo/ImPojo.dart';
import '../util/DateUtil.dart';
import '../util/ImSingleton.dart';
import 'BaseDao.dart';

class ImSensitiveWordDao extends BaseDao {

  final _logger = Logger(
    printer: PrettyPrinter(),
  );

  Future<ImSensitiveWord?> getByWord(String word) async {
    Database? database = getDatabase(); //ImSingleton.getInstance()?.dataBase;
    List? list = await database?.rawQuery("select * from im_sensitive_word where word = ? limit 1", [word]);
    if (list != null && list.length > 0) {
      return Future.value(_toImSensitiveWord(list[0]));
    }
    return Future.value(null);
  }

  Future<List<ImSensitiveWord>> getByWords(List<String> words) async {
    Database? database = getDatabase(); //ImSingleton.getInstance()?.dataBase;

    String param = words.map((p)=> '?').join(",");
    List<dynamic> params = [];
    params.addAll(words);

    List? list = await database?.rawQuery("select * from im_sensitive_word where word in (${param})", params);
    return list != null ? Future.value(list.map((map)=>_toImSensitiveWord(map)).toList()) : Future.value([]);
  }

  Future<List<ImSensitiveWord>> getAll() async {
    Database? database = getDatabase(); //ImSingleton.getInstance()?.dataBase;
    String sql = "select * from im_sensitive_word where status = 1";
    List<Map<String, dynamic?>>? list = await database?.rawQuery(sql);
    return list != null ? Future.value(list.map((map)=>_toImSensitiveWord(map)).toList()) : Future.value([]);
  }

  Future<void> batchInserts(List<ImSensitiveWord> imSensitiveWords) async {
    if (imSensitiveWords.length < 1) return;
    Database? database = ImSingleton.getInstance().dataBase;
    for (var imSensitiveWord in imSensitiveWords) {
      var data = _toInsertValueMap(imSensitiveWord);
      int? id = await database?.insert("im_sensitive_word", data);
      imSensitiveWord.id = id;
    }
  }

  Future<void> batchUpdates(List<ImSensitiveWord> imSensitiveWords) async {
    if (imSensitiveWords.length < 1) return;
    Database? database = getDatabase(); //ImSingleton.getInstance()?.dataBase;
    Batch? batch = database?.batch();
    for (var imSensitiveWord in imSensitiveWords) {
      var data = _toInsertValueMap(imSensitiveWord);
      batch?.update("im_sensitive_word", data, where: "id=?", whereArgs:[imSensitiveWord.id]);
    }
    List<Object?>? list = await batch?.commit();
    _logger.i("batch json: " + jsonEncode(list));
  }

  Map<String, dynamic?> _toInsertValueMap(ImSensitiveWord imSensitiveWord) {
    Map<String, dynamic?> map = {};
    map['word'] = imSensitiveWord.word;
    map["status"]= imSensitiveWord.status?.value;
    map['createTime'] = DateUtil.formatDate(imSensitiveWord.createTime);
    map['updateTime'] = DateUtil.formatDate(imSensitiveWord.updateTime);
    return map;
  }

  ImSensitiveWord _toImSensitiveWord(Map<String, dynamic> map) {
    var sensitiveWord = ImSensitiveWord(word: MapUtil.getString(map, "word"));
    sensitiveWord.id = MapUtil.getInt(map, "id");
    sensitiveWord.status = ImSensitiveWordStatus.ofValue(MapUtil.getInt(map, "status"));
    sensitiveWord.createTime = MapUtil.getDate(map, "createTime");
    sensitiveWord.updateTime = MapUtil.getDate(map, "updateTime");
    return sensitiveWord;
  }
}